package com.wwj.app.v1;

import com.github.pagehelper.Page;
import com.wwj.core.wawa.entity.PlayerAddress;
import com.wwj.core.wawa.service.IPlayerAddressService;
import com.wwj.framework.controller.BaseController;
import com.wwj.framework.utils.ReturnData;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by Administrator on 2017/11/3.
 */
@Controller
@RequestMapping("/v1/address")
public class PlayerAddressController extends BaseController {

    @Resource
    private IPlayerAddressService playerAddressService;

    @RequestMapping("/{id}")
    @ResponseBody
    public ReturnData addressInfo(@PathVariable int id) {
        ReturnData returnData = new ReturnData();
        try {
            PlayerAddress address = playerAddressService.findById(id);
            if (address == null) {
                returnData.failure("找不到地址信息!");
                return returnData;
            }

            returnData.success(address);
        } catch (Exception e) {
            logger.error("", e);
            returnData.failure("系统错误,请联系客服");
        }
        return returnData;
    }

    @RequestMapping("/list")
    @ResponseBody
    public ReturnData list(@RequestParam int playerId) {
        ReturnData returnData = new ReturnData();
        try {
            List<PlayerAddress> addressList = playerAddressService.selectPlayerAddressesByPlayerId(playerId);
            returnData.success(addressList);
        } catch (Exception e) {
            logger.error("", e);
            returnData.failure("系统错误,请联系客服");
        }
        return returnData;
    }

    @RequestMapping("/add")
    @ResponseBody
    public ReturnData addPlayerAddress(PlayerAddress address) {
        ReturnData returnData = new ReturnData();
        try {
            playerAddressService.addPlayerAddress(address);
            returnData.success();
        } catch (Exception e) {
            logger.error("", e);
            returnData.failure("系统错误,请联系客服");
        }
        return returnData;
    }

    @RequestMapping("/edit")
    @ResponseBody
    public ReturnData editPlayerAddress(PlayerAddress address) {
        ReturnData returnData = new ReturnData();
        try {
            playerAddressService.modifyPlayerAddress(address);
            returnData.success();
        } catch (Exception e) {
            logger.error("", e);
            returnData.failure("系统错误,请联系客服");
        }
        return returnData;
    }

    @RequestMapping("/remove/{id}")
    @ResponseBody
    public ReturnData removePlayerAddress(@PathVariable int id) {
        ReturnData returnData = new ReturnData();
        try {
            playerAddressService.remove(id);
            returnData.success();
        } catch (Exception e) {
            logger.error("", e);
            returnData.failure("系统错误,请联系客服");
        }
        return returnData;
    }
}
